/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package unilearn.dao.hibernate;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import unilearn.dao.SubjectStudentDAO;
import unilearn.vo.SubjectStudent;

/**
 *
 * @author TerryLok2011
 */
public class SubjectStudentDAOImpl extends HibernateDAO implements SubjectStudentDAO {

    public SubjectStudentDAOImpl() {
        super();
    }

    public SubjectStudentDAOImpl(SessionFactory sessionFactory) {
        super(sessionFactory);
    }

    @Override
    public void setSessionFactory(SessionFactory sessionFactory) {
        super.setSessionFactory(sessionFactory);
    }

    @Override
    public List<SubjectStudent> getAll() throws Exception {
        return super.getAll(SubjectStudent.class.getName());
    }

    @Override
    public SubjectStudent get(int id) throws Exception {
        Session s = sessionFactory.openSession();
        Transaction t = s.beginTransaction();

        try {
            Query query = s.createQuery("from " + SubjectStudent.class.getName() + " ss where sm.id = " + id);
            List<SubjectStudent> sss = query.list();
            if ((sss == null) || (sss.isEmpty())) {
                return null;
            }

            return sss.get(0);
        } finally {
            t.commit();
            s.close();
        }
    }

    @Override
    public List<SubjectStudent> find(SubjectStudent subjectStudent) throws Exception {
        return (List<SubjectStudent>) super.find(subjectStudent);
    }

    @Override
    public SubjectStudent insert(SubjectStudent subjectStudent) throws Exception {
        super.insert(subjectStudent);
        return subjectStudent;
    }

    @Override
    public SubjectStudent update(SubjectStudent subjectStudent) throws Exception {
        super.update(subjectStudent);
        return subjectStudent;
    }

    @Override
    public void delete(SubjectStudent subjectStudent) throws Exception {
        super.delete(subjectStudent);
    }
}
